import { createRouter, createWebHashHistory } from 'vue-router'
// 登录
import login from '../views/login/login.vue'
// 主页
import home from '../views/home/home.vue'
// 商品分类
import classification from '../views/classification/classification.vue'
// 商品列表
import product_category from "../views/product_category/components/product_category.vue"
// 商品详情
import product from '../views/product/product.vue'
// 用户注册
import register from '../views/register/register.vue'
// 忘记密码
import forget from '../views/forget/forget.vue'
// 用户中心
import member from "../views/member/member";
import Index from '../views/member/pages/Index'
import MyOrder from '../views/member/pages/MyOrder';
import ChangePaw from "../views/member/pages/ChangePaw";
import ChangeInformation from "../views/member/pages/ChangeInformation";
import Address from "../views/member/pages/Address";
import DetailsAddress from "../views/member/pages/DetailsAddress";
import GoodsCollection from "../views/member/pages/GoodsCollection";
import ShopCollection from "../views/member/pages/ShopCollection";
import ShowOneOrder from "../views/member/pages/ShowOneOrder";
import AfterSales from "../views/member/pages/AfterSales";
import addAddress from "../views/member/pages/addAddress.vue";
// 商家注册
import merchant from '../views/register/merchant.vue'
// 美妆分类
import beauty from '../views/product_category/beauty.vue'
// 大电器分类
import appliances from '../views/product_category/appliances.vue'
// 数码通讯
import digital from '../views/product_category/digital.vue'
// 查询商品
import search from '../views/product_category/search.vue'
// 空白页面
import blank from '../views/blank.vue'
// 店铺详情
import store from '../views/store/store.vue'
// 购物车
import cart from '../views/cart/cart.vue'
// 订单页面
import createorder from '../views/order/createOrder.vue'
// 企业认证
import companyauth from '../views/companyauth/companyauth.vue'


const routes = [
  {path: '/', redirect:'/home'},
  {path: '/cart', component: cart},
  {path: '/home', component: home},
  {path: '/classification', component: classification},
  {path: '/login', component: login},
  {path: '/forget', component: forget},
  {path: '/merchant', component: merchant},
  {path: '/companyauth', component: companyauth},
  {path: '/product_category', component:product_category},
  {path: '/createorder', component:createorder},
  {path: '/beauty', component:beauty},
  {path: '/appliances', component:appliances},
  {path: '/digital', component:digital},
  {path: '/searchshop', component:search},
  {path: '/product', component: product},
  {path: '/register', component: register},
  {path: '/store', component: store },
  {path: '/blank', component: blank},
  {
    path: '/member',
    component: member,
    children:[
      {
        path:'index',
        component: Index,
      },
      {
        path: 'myOrder',
        component: MyOrder,
      },
      {
        path: 'showOneOrder',
        component: ShowOneOrder,
      },
      {
        path: 'changePaw',
        component: ChangePaw,
      },
      {
        path: 'changeInformation',
        component: ChangeInformation,
      },
      {
        path: 'address',
        component: Address,
      },
      {
        path: 'addAddress',
        component: addAddress,
      },
      {
        path:'detailsAddress/:addressId',
        component: DetailsAddress,
        props($route){
          return {addressId:$route.query.addressId}
        }
      },
      {
        path: 'goodsCollection',
        component: GoodsCollection,
      },
      {
        path: 'shopCollection',
        component: ShopCollection,
      },
      {
        path: 'afterSales',
        component: AfterSales,
      }
    ],
  }
]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login' || to.path ==='/home' || to.path ==='/register' || to.path ==='/merchant' || to.path ==='/forget'){
    return next()
  }
  
  const token = localStorage.getItem('token')
  if(!token){
    return next('login')
  }else{
    if(to.path === '/search'){
      if(sessionStorage.getItem('search')){
        next()
      }
    }else{
      next()
    }
  }
  
})
router.afterEach(() => {
  window.scrollTo(0,0);
})
export default router
